/*
 * JTalkerClientPluginsInfo.java
 *
 * Created on 9. Dezember 2005, 11:32
 */

package com.jtalker.client;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Method;

import javax.swing.AbstractButton;
import javax.swing.AbstractListModel;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * 
 * @author chrmac@gmx.de
 */
public class JTalkerClientPluginsInfo extends JFrame {
	private JTalkerClientFront parent = null;
	private JTalkerClientPluginLoader myLoader = null;

	/** Creates new form JTalkerClientPluginsInfo */
	public JTalkerClientPluginsInfo(JTalkerClientFront parent) {
		this.parent = parent;

		this.initComponents();
		// this.setPreferredSize(new Dimension(400, 200));
		//
		// this.updatePluginList(null);
	}

	/** Updates the plugin list */
	public boolean updatePluginList(String searchURL) {
		final String[] tmpListItems;

		try {
			if (searchURL != null) {
				this.myLoader = this.parent.getPluginLoader();
				// this.myLoader = new JTalkerClientPluginLoader(this.parent);
				this.myLoader.initPlugins(searchURL);
				tmpListItems = this.myLoader.getPluginNames();
			} else {
				this.myLoader = this.parent.getPluginLoader();
				tmpListItems = this.myLoader.getPluginNames();
			}

			if (tmpListItems == null || tmpListItems.length == 0) {
				JOptionPane.showMessageDialog(this, "No plugins found in:\n" + searchURL, "Scanning directory",
						JOptionPane.INFORMATION_MESSAGE);
				this.setVisible(false);
				return false;
			}

			jListAllPlugIns.setModel(new AbstractListModel() {
				private static final long serialVersionUID = 1L;

				String[] strings = tmpListItems;

				@Override
				public int getSize() {
					if (strings == null) {
						return 0;
					}
					return strings.length;
				}

				@Override
				public Object getElementAt(int i) {
					if (strings[i] != null) {
						return strings[i];
					} else {
						return "---";
					}
				}

			});
		} catch (Exception e) {
			System.err.println("*** Error while reloading plugins: " + e.toString());
			return false;
		}

		return true;
	}

	/**
	 * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The
	 * content of this method is always regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
	private void initComponents() {
		jPanelSouth = new JPanel();
		jButtonOK = new JButton();
		jScrollPaneAllPlugIns = new JScrollPane();
		jListAllPlugIns = new JList();
		jPanelMainContent = new JPanel();
		jLabelName = new JLabel();
		jLabelNameDis = new JLabel();
		jLabelClasss = new JLabel();
		jLabelDesc = new JLabel();
		jLabelName = new JLabel();
		jLabelClass = new JLabel();
		jScrollPaneDesc = new JScrollPane();
		jTextAreaDesc = new JTextArea();
		jScrollPaneMethods = new JScrollPane();
		jListMethods = new JList();
		jLabelIcon = new JLabel();
		jCheckBoxShortcut = new JCheckBox();

		setTitle("PlugIn Overview");
		setName("JFramePluginsInfo");
		setResizable(false);
		jPanelSouth.setPreferredSize(new Dimension(545, 40));
		jButtonOK.setText("OK");
		jButtonOK.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				clickOK(evt);
			}
		});

		jPanelSouth.add(jButtonOK);
		getContentPane().add(jPanelSouth, BorderLayout.SOUTH);
		jListAllPlugIns.setPreferredSize(new Dimension(150, 100));
		jListAllPlugIns.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent evt) {
				changePlugins(evt);
			}
		});

		jScrollPaneAllPlugIns.setViewportView(jListAllPlugIns);

		getContentPane().add(jScrollPaneAllPlugIns, BorderLayout.WEST);

		jPanelMainContent.setLayout(null);

		jPanelMainContent.setPreferredSize(new Dimension(80, 320));
		jLabelName.setHorizontalAlignment(SwingConstants.RIGHT);
		jLabelName.setText("Name:");
		jLabelName.setVerticalAlignment(SwingConstants.TOP);
		jPanelMainContent.add(jLabelName);
		jLabelName.setBounds(0, 10, 80, 30);

		jLabelClasss.setHorizontalAlignment(SwingConstants.RIGHT);
		jLabelClasss.setText("Main-Class:");
		jLabelClasss.setVerticalAlignment(SwingConstants.TOP);
		jPanelMainContent.add(jLabelClasss);
		jLabelClasss.setBounds(0, 40, 80, 30);

		jLabelDesc.setHorizontalAlignment(SwingConstants.RIGHT);
		jLabelDesc.setText("Description:");
		jLabelDesc.setVerticalAlignment(SwingConstants.TOP);
		jPanelMainContent.add(jLabelDesc);
		jLabelDesc.setBounds(0, 70, 90, 30);

		jLabelNameDis.setText("");
		jLabelNameDis.setVerticalAlignment(SwingConstants.TOP);
		jPanelMainContent.add(jLabelNameDis);
		jLabelNameDis.setBounds(90, 10, 170, 30);

		jLabelClass.setText("");
		jLabelClass.setVerticalAlignment(SwingConstants.TOP);
		jPanelMainContent.add(jLabelClass);
		jLabelClass.setBounds(90, 40, 170, 30);

		jScrollPaneDesc.setAutoscrolls(true);
		jTextAreaDesc.setColumns(20);
		jTextAreaDesc.setEditable(false);
		jTextAreaDesc.setLineWrap(true);
		jTextAreaDesc.setRows(5);
		jTextAreaDesc.setText("");
		jTextAreaDesc.setWrapStyleWord(true);
		jScrollPaneDesc.setViewportView(jTextAreaDesc);

		jPanelMainContent.add(jScrollPaneDesc);
		jScrollPaneDesc.setBounds(100, 70, 280, 50);

		jScrollPaneMethods.setBorder(BorderFactory.createTitledBorder("Methods"));
		jScrollPaneMethods.setViewportView(jListMethods);

		jPanelMainContent.add(jScrollPaneMethods);
		jScrollPaneMethods.setBounds(10, 130, 370, 140);

		jLabelIcon.setText("NO ICON");
		jLabelIcon.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		jPanelMainContent.add(jLabelIcon);
		jLabelIcon.setBounds(270, 10, 60, 50);

		jCheckBoxShortcut.setText("Create a shortcut for this plugin in the main window");
		jCheckBoxShortcut.setToolTipText("Creates a button in JTalkers toolbar");
		jCheckBoxShortcut.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
		jCheckBoxShortcut.setMargin(new Insets(0, 0, 0, 0));
		jPanelMainContent.add(jCheckBoxShortcut);
		jCheckBoxShortcut.setBounds(10, 280, 370, 15);
		jCheckBoxShortcut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				generateShortcut(evt);
			}
		});

		getContentPane().add(jPanelMainContent, BorderLayout.CENTER);

		pack();
	}// </editor-fold>//GEN-END:initComponents

	/** Embeding plugin was checked */
	private void generateShortcut(ActionEvent evt) {
		String selectedPlugin = (String) this.jListAllPlugIns.getSelectedValue();

		if (this.jCheckBoxShortcut.isSelected()) {
			this.parent.theClient.addPluginShortcut(selectedPlugin);
		} else {
			this.removeEmbeddedStatusFor(selectedPlugin);
		}

		this.parent.updatePluginShortcuts();
	}

	/** Ok was clicked */
	private void clickOK(ActionEvent evt) {
		String selectedPlugin = (String) this.jListAllPlugIns.getSelectedValue();
		JTalkerClientPlugInInterface tmpPlugin = this.myLoader.getPluginByName(selectedPlugin);

		this.dispose();
	}

	private void changePlugins(ListSelectionEvent e) {// GEN-FIRST:event_jListAllPlugInsValueChanged
		String selectedPlugin = (String) this.jListAllPlugIns.getSelectedValue();
		JTalkerClientPlugInInterface tmpPlugin = this.myLoader.getPluginByName(selectedPlugin);

		if (tmpPlugin == null) {
			return;
		}
		this.jLabelNameDis.setText(selectedPlugin);
		this.jLabelIcon.setText("");
		this.jLabelIcon.setOpaque(true);
		this.jLabelIcon.setIcon(tmpPlugin.getIcon());
		this.jLabelClass.setText(tmpPlugin.getClass().getName());
		this.jTextAreaDesc.setText(tmpPlugin.getDescription());

		Method[] allMethods = tmpPlugin.getCommandMethods();
		final String[] tmpMethods = new String[allMethods.length];

		for (int i = 0; i < allMethods.length; i++) {
			tmpMethods[i] = allMethods[i].getName();
		}

		jListMethods.setModel(new AbstractListModel() {
			private static final long serialVersionUID = 1L;
			String[] strings = tmpMethods;

			public int getSize() {
				return strings.length;
			}

			public Object getElementAt(int i) {
				return strings[i];
			}
		});

		if (this.isPluginEmbedded(selectedPlugin)) {
			this.jCheckBoxShortcut.setSelected(true);
		} else {
			this.jCheckBoxShortcut.setSelected(false);
		}
	}// GEN-LAST:event_jListAllPlugInsValueChanged

	/** Checks, if selected plug-in is embedded in the clients toolbar */
	private boolean isPluginEmbedded(String pluginName) {
		return this.parent.theClient.isPluginShortcut(pluginName);
	}

	/** Removes an embedded plugin from the list */
	private boolean removeEmbeddedStatusFor(String pluginName) {
		return this.parent.theClient.removePluginShortcut(pluginName);
	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	/*
	 * public static void main(String args[]) { EventQueue.invokeLater(new Runnable() { public void run() { new
	 * JTalkerClientPluginsInfo().setVisible(true); } }); }
	 */

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private JButton jButtonOK;
	private JCheckBox jCheckBoxShortcut;
	private JLabel jLabelClasss;
	private JLabel jLabelClass;
	private JLabel jLabelDesc;
	private JLabel jLabelIcon;
	private JLabel jLabelName;
	private JLabel jLabelNameDis;
	private JList jListAllPlugIns;
	private JList jListMethods;
	private JPanel jPanelSouth;
	private JPanel jPanelMainContent;
	private JScrollPane jScrollPaneAllPlugIns;
	private JScrollPane jScrollPaneDesc;
	private JScrollPane jScrollPaneMethods;
	private JTextArea jTextAreaDesc;
	// End of variables declaration//GEN-END:variables

}
